#include <windef.h>
#include <winbase.h>
#include <string.h>
#include <stdio.h>
#include <ws2tcpip.h>
#include <io.h>
#include <locale.h>

void test()
{
    char m_szWatchDirectory[] = "D:\\workspace\\my_project";
    HANDLE m_hDirectory = CreateFile(m_szWatchDirectory, GENERIC_READ | GENERIC_WRITE | FILE_LIST_DIRECTORY,
                                     FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_EXISTING,
                                     FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED, NULL);

    if (m_hDirectory == INVALID_HANDLE_VALUE)
    {
        DWORD dwErr = GetLastError();
        return;
    }

    char notify[1024];
    memset(notify, 0, sizeof(notify));
    FILE_NOTIFY_INFORMATION *pNotification = (FILE_NOTIFY_INFORMATION *)notify;
    DWORD BytesReturned = 0;

    while (TRUE)
    {
        ZeroMemory(pNotification, sizeof(notify));

        BOOL watch_state = ReadDirectoryChangesW(m_hDirectory,
                                                 &notify,
                                                 sizeof(notify),
                                                 TRUE,                                                         //监控子目录
                                                 FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_LAST_WRITE, //FILE_NOTIFY_CHANGE_DIR_NAME FILE_NOTIFY_CHANGE_CREATION FILE_NOTIFY_CHANGE_SIZE
                                                 (LPDWORD)&BytesReturned,
                                                 NULL,
                                                 NULL);

        if (GetLastError() == ERROR_INVALID_FUNCTION)
        {
            printf("文件监控，系统不支持! %s", m_hDirectory);
            break;
        }
        else if (watch_state == FALSE)
        {
            DWORD dwErr = GetLastError();
            printf("文件监控，监控失败! %s (LastError: %d)", m_hDirectory, dwErr);
            break;
        }
        else if (GetLastError() == ERROR_NOTIFY_ENUM_DIR)
        {
            printf("监控文件%s，内存溢出! ", m_hDirectory);
            continue;
        }
        else
        {
            if (setlocale(LC_CTYPE, "Chinese-simplified") != NULL)
            { /*设置为本地环境变量定义的locale*/
                fprintf(stderr, "can't set the locale\n");
            }
            wchar_t *head = "文件监控";
            printf("head: %ws\n", head);
            int filename_size = pNotification->FileNameLength / sizeof(wchar_t) + 1;
            wchar_t szFileName[filename_size];
            sprintf(szFileName, "%ws", pNotification->FileName);
            printf("文件监控: %ws", pNotification->FileName);
            wchar_t filename[1];
            strcpy(filename, pNotification->FileName);
            strcat(filename, "_123");
            FILE *handle = _wfopen(pNotification->FileName, "r");

            if (!access(pNotification->FileName, 0))
            {
                printf("file exists");
            }
            else
            {
                printf("file don't exists");
            }
            fclose(handle);
            // strncpy(szFileName, &(pNotification->FileName), filename_sizeF);
            // memcpy(szFileName, pNotification->FileName, filename_size-1);
            //这里主要就是检测返回的信息，(FILE_NOTIFY_INFORMATION)
            // if (pNotification->Action == FILE_ACTION_ADDED)
            // {
            //     printf("文件监控，新增文件! %s\\%ws", m_szWatchDirectory, pNotification->FileName);
            // }
            // else if (pNotification->Action == FILE_ACTION_REMOVED)
            // {
            //     printf("文件监控，删除文件! %s\\%ws", m_szWatchDirectory, pNotification->FileName);
            // }
            // else if (pNotification->Action == FILE_ACTION_MODIFIED)
            // {
            //     printf("文件监控，修改文件! %s\\%ws", m_szWatchDirectory, pNotification->FileName);
            // }
            // else if (pNotification->Action == FILE_ACTION_RENAMED_OLD_NAME)
            // {
            //     printf("文件监控，重命名文件! %s\\%ws", m_szWatchDirectory, pNotification->FileName);
            // }
            // else if (pNotification->Action == FILE_ACTION_RENAMED_NEW_NAME) //还没出现过这种情况
            // {
            //     printf("文件监控，重命名文件2! %s\\%ws", m_szWatchDirectory, pNotification->FileName);
            // }
            // //PostMessage通知主线程
        }
    }

    CloseHandle(m_hDirectory);
}

void main()
{
    test();
}